class EditionsController < ApplicationController
  filter_access_to :all
  
  #If AJAX request, use the slim layout (no header or footer)
  #Otherwise use the main layout
  layout Proc.new { |controller| controller.request.xhr? ? 'slim' : 'main' }
  
  #
  #
  def show
    if params[:release_id]
      @release = Release.find(params[:release_id])
      @edition = @release.editions.find(params[:id])
    else
      @edition = Edition.find(params[:id])
      @release = @edition.release
    end

    store_location
    session[:edit_mode] = false
  end

  #
  #
  def edit
    @release = Release.find(params[:release_id])
    @edition = @release.editions.find(params[:id])
    store_location
    session[:edit_mode] = true

    respond_to do |format|
      format.html
    end
  end

  #
  #
  def new
    @edition = Edition.new

    respond_to do |format|
      format.html
    end
  end

  #
  #
  def create
    @edition = Edition.new(params[:edition])

    respond_to do |format|
      if @edition.save
        flash[:notice] = 'Successfully Added.'
        format.html { redirect_to release_edition_path(@edition.release, @edition) }
      else
        flash[:notice] = 'Edition not added.'
        format.html { render :action => :new }
      end
    end
  end

  def update
    @edition = Edition.find(params[:id])

    if params[:images] && params[:images][:image]
      @edition.images.build(params[:images])
    end

    if @edition.update_attributes(params[:edition])
      flash[:notice] = "Successfully updated"
      redirect_to_request_uri
    else
      render :action => 'edit'
    end
  end
end
